public class Solution42 {
    public int trap(int[] height){
        int left = 0,right = height.length - 1;
        int l_max = 0,r_max = 0;
        int res = 0;
        while(left < right){
            //l_max:左指针左侧最高的柱子
            l_max = Math.max(l_max,height[left]);
            //r_max:右指针右侧最高的柱子
            r_max = Math.max(r_max,height[right]);

            if(l_max < r_max){
                res += l_max - height[left];
                left++;
            }else{
                res += r_max - height[right];
                right--;
            }
        }
        return res;
    }
}
